/**
 * 
 */
package com.huarui.desktop;

import org.apache.struts2.ServletActionContext;
import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.PagingInfo;

import com.huarui.desktop.model.JsModule;
import com.huarui.desktop.model.ModuleLauncher;
import com.huarui.desktop.service.JsModuleInfoService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author sunpeng
 * 
 */
public class JsModuleInfoAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6603621057973557268L;
	private ModuleLauncher launcher;
	private JsModuleInfoService jsModuleInfoService;

	public String list() throws Exception {
		ActionContext context = ServletActionContext.getContext();
		int start = super.getIntegerParameter("start", 0);
		int limit = super.getIntegerParameter("limit", 19);
		String moduleId = super.getSingleParameter("moduleId");
		String launcherText = super.getSingleParameter("launcherText");
		int permissionId = super.getIntegerParameter("permissionId", 0);

		ModuleLauncher launcherCondition = new ModuleLauncher();
		launcherCondition.setText(launcherText);
		JsModule condition = new JsModule();
		condition.setModuleId(moduleId);
		condition.setLauncher(launcherCondition);
		condition.setPermissionId(permissionId);

		PagingInfo<JsModule, JsModule> paginInfo = new PagingInfo<JsModule, JsModule>();
		paginInfo.setCondition(condition);
		paginInfo.setStart(start);
		paginInfo.setLimit(limit);

		this.jsModuleInfoService.queryJsModuleInfos(paginInfo);
		context.put("jsonResult", paginInfo);
		return Action.SUCCESS;
	}

	public String add() {
		ActionContext context = ServletActionContext.getContext();
		String moduleId = super.getSingleParameter("moduleId");
		int permissionId = super.getIntegerParameter("permissionId", 0);
		String permissionName = super.getSingleParameter("permissionName");
		String text = super.getSingleParameter("launcher.text");
		String tooltip = super.getSingleParameter("launcher.tooltip");
		String scriptUrl = super.getSingleParameter("scriptUrl");
		String className = super.getSingleParameter("className");
		String initParams = super.getSingleParameter("initParams");
		String stylesheetUrl = super.getSingleParameter("stylesheetUrl");
		String iconCls = super.getSingleParameter("launcher.iconCls");
		String shortcutIconCls = super.getSingleParameter("launcher.shortcutIconCls");
        
		ModuleLauncher launcher = new ModuleLauncher();
		launcher.setText(text);
		launcher.setIconCls(iconCls);
		launcher.setShortcutIconCls(shortcutIconCls);
		launcher.setTooltip(tooltip);
		
		JsModule jsModule = new JsModule();
		jsModule.setModuleId(moduleId);
		jsModule.setPermissionId(permissionId);
		jsModule.setPermissionName(permissionName);
		jsModule.setClassName(className);
		jsModule.setInitParams(initParams);
		jsModule.setScriptUrl(scriptUrl);
		jsModule.setStylesheetUrl(stylesheetUrl);
		jsModule.setLauncher(launcher);
		
		this.jsModuleInfoService.addJsModule(jsModule);
		context.put("jsonResult", jsModule);
		return Action.SUCCESS;
	}


	public String modify() {
		try{
		ActionContext context = ServletActionContext.getContext();
		Integer id = super.getIntegerParameter("id", 0);
		String moduleId = super.getSingleParameter("moduleId");
		int permissionId = super.getIntegerParameter("permissionId", 0);
		String permissionName = super.getSingleParameter("permissionName");
		String text = super.getSingleParameter("launcher.text");
		String tooltip = super.getSingleParameter("launcher.tooltip");
		String scriptUrl = super.getSingleParameter("scriptUrl");
		String className = super.getSingleParameter("className");
		String initParams = super.getSingleParameter("initParams");
		String stylesheetUrl = super.getSingleParameter("stylesheetUrl");
		String iconCls = super.getSingleParameter("launcher.iconCls");
		String shortcutIconCls = super.getSingleParameter("launcher.shortcutIconCls");
        
		ModuleLauncher launcher = new ModuleLauncher();
		launcher.setText(text);
		launcher.setIconCls(iconCls);
		launcher.setShortcutIconCls(shortcutIconCls);
		launcher.setTooltip(tooltip);
		
		JsModule jsModule = new JsModule();
		jsModule.setId(id);
		jsModule.setModuleId(moduleId);
		jsModule.setPermissionId(permissionId);
		jsModule.setPermissionName(permissionName);
		jsModule.setClassName(className);
		jsModule.setInitParams(initParams);
		jsModule.setScriptUrl(scriptUrl);
		jsModule.setStylesheetUrl(stylesheetUrl);
		jsModule.setLauncher(launcher);
		
		this.jsModuleInfoService.modifyJsModule(jsModule);
		context.put("jsonResult", jsModule);}catch(Exception e){
			System.out.println(e);
		}
		return Action.SUCCESS;
	}
	public void setJsModuleInfoService(JsModuleInfoService jsModuleInfoService) {
		this.jsModuleInfoService = jsModuleInfoService;
	}

	public ModuleLauncher getLauncher() {
		return launcher;
	}

	public void setLauncher(ModuleLauncher launcher) {
		this.launcher = launcher;
	}

}
